package edu.zut.cs.network.iountil;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import edu.zut.cs.network.base.BaseMainCase;
import edu.zut.cs.network.entity.Book;

public class WriteToCSV extends BaseMainCase{
	public WriteToCSV(List<Book> list) {
	try {
		byte[] bs={(byte)0xef,(byte)0xbb,(byte)0xbf};//  UTF-8的 编码  默认无BOM 此行写入文件头变带BOM头
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/bookqueue.csv"), "UTF-8"));
		out.write(new String(bs));//写入BOM头
		for (Book i :list) {
			out.write(i.getBookName());
			out.write(",");
			out.write(i.getPosition());
			out.write(",");
			out.write(i.getNumber());
			out.write(",");
			out.write(Long.toString(i.getId()));
			out.newLine();
		}
		out.flush();
		out.close();
		logger.info("写入成功！");
	} catch (IOException e) {
		e.printStackTrace();
	}
	}
}
